﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Data.Sql;
using Oak.Model;

namespace Oak.Core.Data
{
    public class ActPermissionDA
    {

        public List<CoActPermissionInfo> SelectList()
        {
            CoActPermissionInfo p = new CoActPermissionInfo();
            IDbHandler dbh = p.CreateHandler();
            dbh.SetFields(p.Fields);
            DataReader dr = dbh.Select();
            List<CoActPermissionInfo> list = new List<CoActPermissionInfo>();
            while (dr.Read())
            {
                CoActPermissionInfo perm = new CoActPermissionInfo();
                dr.ReadTo(perm);
                list.Add(perm);
            }
            dr.Close();
            return list;
        }

        public List<CoActPermissionInfo> SelectList(CoActPermissionQry qry, out ListAttrInfo listAttr)
        {
            CoActPermissionInfo p = new CoActPermissionInfo();
            CoActAppKeyInfo k = new CoActAppKeyInfo();
            CoActionInfo a = new CoActionInfo();

            IDbHandler dbh = p.CreateHandler();
            dbh.SetFields(p.Fields);
            dbh.SetFields(a.Fields);

            dbh.Join(k).On(k.AppKeyId.Equals(p.AppKeyId));
            dbh.Join(a).On(a.ActionId.Equals(p.ActionId));

            if (!qry.AppKeyId.IsNull)
                dbh.Where(p.AppKeyId.Equals(qry.AppKeyId.Value));

            if (!qry.AppKey.IsNull)
                dbh.Where(k.AppKey.Equals(qry.AppKey.Value));

            if (!qry.Keyword.IsNullOrEmpty)
            {
                CompoundCondition cc = new CompoundCondition();
                cc.Or(a.ActionName.Like(qry.Keyword.Value))
                    .Or(a.Description.Like(qry.Keyword.Value));
                dbh.Where(cc);
            }

            DataReader dr = dbh.Select(qry, out listAttr);
            List<CoActPermissionInfo> list = new List<CoActPermissionInfo>();
            while(dr.Read())
            {
                CoActPermissionInfo perm = new CoActPermissionInfo();
                perm.Action.New();
                dr.ReadTo(perm, perm.Action.Value);
                list.Add(perm);
            }
            dr.Close();
            return list;
        }

        public CoActPermissionInfo SelectActPermission(long appKeyId, long actionId)
        {
            CoActPermissionInfo p = new CoActPermissionInfo();
            IDbHandler dbh = p.CreateHandler();
            dbh.SetFields(p.Fields);
            dbh.Where(p.AppKeyId.Equals(appKeyId));
            dbh.Where(p.ActionId.Equals(actionId));
            DataReader dr = dbh.Select();
            if (dr.Read())
            {
                dr.ReadTo(p);
                dr.Close();
                return p;
            }
            else
            {
                dr.Close();
                return null;
            }
        }

        public int DeleteByItemIds(params long[] itemIds)
        {
            CoActPermissionInfo p = new CoActPermissionInfo();
            IDbHandler dbh = p.CreateHandler();
            dbh.Where(p.ItemId.In(itemIds));
            return dbh.Delete();
        }

    }
}
